package com.d6game.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.NameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import weibo4j.Account;
import weibo4j.Oauth;
import weibo4j.Users;
import weibo4j.examples.oauth2.Log;
import weibo4j.http.AccessToken;
import weibo4j.model.User;
import weibo4j.model.WeiboException;
import weibo4j.org.json.JSONException;

import com.d6game.web.job.AutoReplyJob;
import com.d6game.web.service.UserService;
import com.d6game.web.util.HttpClientUtil;
import com.d6game.web.util.SysConstants;


@Controller
@RequestMapping(value = "/")
public class AuthzController extends BaseController{
	@Autowired
	UserService userService;
	
	@RequestMapping(value="/oauth/sina")
    public ModelAndView sina(HttpServletRequest request, HttpServletResponse response){
		String url = "https://api.weibo.com/oauth2/authorize?client_id="+SysConstants.SINA_CLIENT_ID+"&response_type=code&redirect_uri="+SysConstants.SINA_REDIRECT_URI;
		return new ModelAndView(new RedirectView(url));
    }
	@RequestMapping(value="/oauth/sina/callback")
    public ModelAndView sina_callback(HttpServletRequest request, HttpServletResponse response, HttpSession session){
		String code = request.getParameter("code");
		Oauth oauth = new Oauth();
		AccessToken accessToken = null;
		try {
			accessToken = oauth.getAccessTokenByCode(code);
			if(accessToken != null){
				//sesssion保存accessToken
				session.setAttribute("accessToken", accessToken);
			}
		} catch (WeiboException e1) {
			e1.printStackTrace();
		}
		String access_token = accessToken.getAccessToken();
		Account am = new Account();
		am.client.setToken(access_token);
		weibo4j.org.json.JSONObject uid = new weibo4j.org.json.JSONObject();
		try {
			uid = am.getUid();
		} catch (WeiboException e) {
			e.printStackTrace();
		}
		Users um = new Users();
		um.client.setToken(access_token);
		try {
			User wb_user = um.showUserById(uid.getString("uid").toString());
			//session保存微博用户
			session.setAttribute("wb_user", wb_user);
			Log.logInfo(wb_user.toString());
			com.d6game.web.entity.User user = userService.findUserByOuterid(wb_user.getId());
			if(user == null){
				user = new com.d6game.web.entity.User();
				//user.setEmail("");
				user.setOuterid(wb_user.getId());
				user.setSource("sina");
				user.setUsername(wb_user.getScreenName());
				user.setRole("1");
				userService.saveUser(user);
			}
			session.setAttribute("user", user);
		} catch (WeiboException e) {
			e.printStackTrace();
		}catch (JSONException e) {
			e.printStackTrace();
		}
		new Thread(new AutoReplyJob(access_token)).start();
		return new ModelAndView(new RedirectView("/"));
    }
}
